<template>
  <div class="app-container">
    <el-form :inline="true" :model="getFormDate" class="demo-form-inline">
      <el-form-item>
        <el-input size="mini" v-model="getFormDate.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input size="mini" v-model="getFormDate.url" placeholder="请输入路径"></el-input>
      </el-form-item>
      <!-- <el-form-item>
        <el-select size="mini" v-model="getFormDate.type" clearable placeholder="请选择类型">
          <el-option v-for="item in types" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button @click="getPermission" size="mini">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button style="margin-bottom: 5px;" size="mini" @click="add">新增</el-button>
    <!-- <el-button style="margin-bottom: 5px;" size="mini" @click="handlerBatchDelete">批量删除</el-button> -->
    <el-table size="mini" :data="tableData" @selection-change="handleSelectionChange" style="width: 100%" row-key="id"
      lazy :tree-props="{ children: 'sonPermissionList', hasChildren: 'hasChildren' }">
      <!-- <el-table-column type="selection" width="55">
      </el-table-column> -->
      <el-table-column prop="name" label="名称">
      </el-table-column>
      <el-table-column prop="metaName" label="路由名称">
      </el-table-column>
      <el-table-column prop="url" label="路径">
      </el-table-column>
      <el-table-column prop="permissionValue" label="权限值">
      </el-table-column>
      <el-table-column prop="icon" label="图标">
      </el-table-column>
      <el-table-column prop="type" label="类型">
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="success">启用</el-tag>
          <el-tag v-else type="danger">禁止</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="level" label="级别">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="float: right;margin-top: 5px;" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="pageSize"
      layout="prev, pager, next, jumper" :total="totalNum">
    </el-pagination>
    <el-dialog title="添加权限" :visible.sync="dialogVisibleAdd" width="30%">
      <el-form :model="addFormData" label-width="80px" ref="addFormData">
        <el-form-item label="路由级别">
          <el-select v-model="addFormData.level" placeholder="请选择路由级别">
            <el-option v-for="item in levels" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-show="addFormData.level != 1 && addFormData.level != null" label="上级路由">
          <el-select v-model="addFormData.pid" placeholder="请选择上级路由">
            <el-option v-for="item in parentRouters" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="addFormData.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="路由名称">
          <el-input v-model="addFormData.metaName" placeholder="请输入路由名称"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="addFormData.url" placeholder="请输入路径"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="addFormData.type" placeholder="请选择类型">
            <el-option v-for="item in types" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限值">
          <el-input v-model="addFormData.permissionValue" placeholder="请输入权限值"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-select v-model="addFormData.icon" placeholder="请选择图标">
            <el-option v-for="item in icons" :key="item.value" :label="item.label" :value="item.value">
              <svg-icon :icon-class="item.value" style="margin-right: 5px;"/> {{ item.label }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="createPermission">确认</el-button>
          <el-button @click="resetFormAdd('addFormData')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog title="修改权限" :visible.sync="dialogVisibleUpdate" width="30%">
      <el-form :model="updateFormData" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="updateFormData.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="路由名称">
          <el-input v-model="updateFormData.metaName" placeholder="请输入路由名称"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="updateFormData.url" placeholder="请输入路径"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="updateFormData.type" placeholder="请选择类型">
            <el-option v-for="item in types" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限值">
          <el-input v-model="updateFormData.permissionValue" placeholder="请输入权限值"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-select v-model="updateFormData.icon" placeholder="请选择图标">
            <el-option v-for="item in icons" :key="item.value" :label="item.label" :value="item.value">
              <svg-icon :icon-class="item.value" style="margin-right: 5px;"/> {{ item.label }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="updatePermission">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionByPage, addPermission, getPermissionById, 
        deletePermission, updatePermissionById, batchDeletePermission,
        getPermissionByLevel } from "@/api/system.js"
export default {
  data() {
    return {
      list: null,
      listLoading: true,
      getFormDate: {},
      tableData: [],
      icons: [
        {
          value: 'dashboard',
          label: 'dashboard'
        },
        {
          value: 'example',
          label: 'example'
        },
        {
          value: 'eye-open',
          label: 'eye-open'
        },
        {
          value: 'eye',
          label: 'eye'
        },
        {
          value: 'form',
          label: 'form'
        },
        {
          value: 'link',
          label: 'link'
        },
        {
          value: 'nested',
          label: 'nested'
        },
        {
          value: 'password',
          label: 'password'
        },
        {
          value: 'table',
          label: 'table'
        },
        {
          value: 'tree',
          label: 'tree'
        },
        {
          value: 'user',
          label: 'user'
        }
      ],
      parentRouters: [],
      multipleSelection: [],
      types: [
        { value: 1, label: "目录" },
        { value: 2, label: "菜单" },
        { value: 3, label: "按钮" }
      ],
      levels: [
        { value: 1, label: "一级" },
        { value: 2, label: "二级" },
        { value: 3, label: "三级" }
      ],
      ids: [],
      dialogVisibleAdd: false,
      dialogVisibleUpdate: false,
      dialogVisibleSetRole: false,
      updateFormData: {},
      addFormData: {},
      pageSize: 10,
      currentPage: 1,
      totalNum: 0,
    }
  },
  created() {
    this.initTableData()
  },
  watch: {
    'addFormData.level'(newVal, oldVal) {
      getPermissionByLevel(newVal).then(res => {
        if(res.data.code == 200){
          this.parentRouters = res.data.data
        }
      })
    }
  },
  methods: {
    setRoles(index, row) {
      this.checkedRoles = []
      if (row.roles) {
        this.checkedRoles = row.roles.map(item => {
          return item.id
        })
      }
      this.dialogVisibleSetRole = true
      this.updateId = row.id
    },
    resetFormAdd(formName) {
      this.addFormData = {}
      this.$refs[formName].resetFields();
    },
    createPermission() {
      addPermission(this.addFormData).then(res => {
        if (res.data.code == 200) {
          this.$message.success("添加成功")
          this.dialogVisibleAdd = false
          this.initTableData();
        }
      })
    },
    updatePermission() {
      updatePermissionById(this.updateFormData).then(res => {
        if (res.data.code == 200) {
          this.$message.success("修改成功")
          this.dialogVisibleUpdate = false
          this.initTableData();
        }
      })
    },
    handlerBatchDelete() {
      if (this.multipleSelection.length > 0) {
        this.ids = this.multipleSelection.map(item => {
          return item.id
        })
        batchDeletePermission(this.ids).then(res => {
          if (res.data.code == 200) {
            this.$message.success("批量删除成功")
            this.initTableData()
          }
        })
      } else {
        this.$message.warning("请先选择删除项")
      }
    },
    initTableData(page) {
      let senddata = {
        ...this.getFormDate,
        size: this.pageSize,
        page: page ? page : this.currentPage
      }
      getPermissionByPage(senddata).then(res => {
        if (res.data.code == 200) {
          this.tableData = res.data.data.records
          this.totalNum = res.data.data.total
        }
      })
    },
    add() {
      this.addFormData = {}
      this.dialogVisibleAdd = true
    },
    handleEdit(index, row) {
      this.dialogVisibleUpdate = true
      getPermissionById(row.id).then(res => {
        if (res.data.code == 200) {
          this.updateFormData = res.data.data
        }
      })
    },
    handleDelete(index, row) {
      this.$confirm('此操作可能导致它以及子菜单被删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePermission(row.id).then(res => {
          if (res.data.code == 200) {
            this.$message.success("删除成功")
            this.initTableData();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    getPermission() {
      this.initTableData();
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.initTableData(val)
    },
    handleCheckAllChange(val) {
      this.checkedRoles = val ? this.roleIds : [];
      this.isIndeterminate = false;
    },
    handleCheckedRolesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.rolesOptions.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.rolesOptions.length;
    },
    setRolesOk() {
      if (this.checkedRoles.length > 0) {
        setRoles(this.updateId, this.checkedRoles).then(res => {
          if (res.data.code == 200) {
            this.$message.success("角色设置成功")
            this.dialogVisibleSetRole = false
            this.initTableData();
          }
        })
      } else {
        this.$message.warning("请先选择角色")
      }
    }
  }
}
</script>

<style>
.el-dialog {
  position: absolute;
  top: -40px;
  margin-left: 550px;
}
</style>